package cn.util;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

public class Commons {
	private Commons() {
	}

	// 转日期为年龄
	public static Integer converteDateToAge(Timestamp date) {
		if (date == null) {
			return 0;
		}
		// 当前系统时间
		Calendar cl = Calendar.getInstance();
		int year0 = cl.get(Calendar.YEAR);
		// 指定的日期时间
		cl.setTime(date);
		int year1 = cl.get(Calendar.YEAR);
		return year0 - year1 == 0 ? 1 : year0 - year1;
	}

	// 转首字母为大写
	public static String upperCaseFirstChar(String name) {
		// tomson--T+omson -->Tomson
		if (name != null && !name.equals("")) {
			return (name.charAt(0) + "").toUpperCase() + name.substring(1);
		}
		return null;
	}

	// 获得随机的文件名称
	public static String getFileName(String string) {
		return UUID.randomUUID().toString().substring(0, 5) + string;
	}

	// 判断文件是否是图片
	public static boolean isPicture(String string) {
		if (string == null) {
			return false;
		}
		List<String> list = new ArrayList<String>(
				Arrays.asList("jpg", "JGP", "JPEG", "jpeg", "PNG", "png", "gif", "GIF", "BMP", "bmp"));
		return list.contains(string);
	}

	// 判断文件是否符合尺寸：5M
	public static boolean isSiutSize(Long size) {
		if (size <= 0 || size > 5 * 1024 * 1024) {
			return false;
		}
		return true;
	}
}
